layui.use(['table',"jquery"], function(){
    var table = layui.table;
    var $=layui.jquery;
    var form = layui.form
    table.render({
      elem: '#menu'
      ,url:'./getMenu'
      ,toolbar: '#toolbarDemo'
      ,title: '用户数据表',
      method:"post"
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field:'m_id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
        ,{field:'m_name', title:'菜单名称', width:120, edit: 'text'}
        ,{field:'m_url', title:'菜单路径', width:150, edit: 'text'}
        ,{field:'m_num', title:'菜单编号', width:80, edit: 'text', sort: true}
        ,{field:'m_status', title:'菜单状态', width:100,templet: '#switchTpl'}
        ,{field:'m_parent', title:'父级菜单', edit: 'text'}
        ,{field:'m_icon', title:'菜单图标', width:80, sort: true, edit: 'text'}
        ,{field:'m_orderby', title:'菜单序号', width:120, edit: 'text'}
        ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
      ]]
      ,page: true
      
    });
      //监听菜单状态操作
     
      form.on('switch(sexDemo)', function(obj){
      
      let  status=obj.elem.checked?1:0
      // console.log(this)
       //console.log(status)
       $.ajax({
         url:"./updateStatus",
         type:"post",
         data:{mId:this.value,m_status:status},
         dataType:"json"
       }).then(function(res){
         if(res.status==1){
          layer.tips(res.msg);
         }
       })
         
       });
    
    //头工具栏事件
    table.on('toolbar(menu)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
        layer.open({
          type: 2,
          title: 'layer mobile页',
          shadeClose: true,
          shade: 0.8,
          area: ['600px', '90%'],
          content: './addmenu' //iframe的url
        }); 
        break;
       
      };
    });
    
    //监听行工具事件
    table.on('tool(menu)', function(obj){
      var data = obj.data;
     console.log(obj)
      if(obj.event === 'del'){

        //发送ajax 删除数据库数据
        $.ajax({
            url:"./deleData",
            data:{mId:obj.data.m_id},
            type:"post",
            dataType:"json"
        }).then(function(res){
            if(res.status==1){
                obj.del();
            }
        })
        
      }
    });
    //监听单元格编辑
    table.on('edit(menu)', function(obj){
        // var value = obj.value //得到修改后的值
        // ,data = obj.data //得到所在行所有键值
        // ,field = obj.field; //得到字段
        // layer.msg('[ID: '+ data.m_id +'] ' + field + ' 字段更改为：'+ value);
        console.log(obj.data)
        $.ajax({
            url:"./updateData",
            method:"post",
            data:obj.data,
            dataType:"json"
        }).then(function(res){
            if(res.status==1){
                layer.msg("修改成功");
            }else{
                layer.msg("修改失败");
            }
        })
      });
     
  });